<?php 
namespace Admin\Controller;
use Think\Controller;
class activitysController extends BaseController {

	/*public function sportlist(){
		$sportModel=M('sport');//表明,conf中定义前缀
		$sport=$sportModel->order('sportid asc')->select(); //desc降序 asc升序

		//dump($sport);
		$this->sport=$sport;//name属性对应的值 $this->对应值=$sport;
			
		$this->display();
	}
*/

public function activitylist(){
    $activitysModel=M('activitys');//表明,conf中定义前缀
    $activitys=$activitysModel->table('superadmin s, activitys t ' )->where("t.spadmin_id=s.spadmin_id ")->field('spadmin_name,t.*')->order('activity_id asc')->select(); //desc降序 asc升序

  
    $this->activitys=$activitys;//name属性对应的值 $this->对应值=$sport;
      
 $this->display();
  }


Public function adddactivity(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['picture']['size']);
          
        //没有头像
        $activityModel = M('activitys'); // 实例化User模型
        $createresult=$activityModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
            
                $activityModel->activity_name = trim(I('post.activity_name'));
                $activityModel->activity_address = trim(I('post.activity_address'));
                $activityModel->activity_money = trim(I('post.activity_money'));  
                $activityModel->activity_people_number = trim(I('post.activity_people_number'));
                $activityModel->activity_tel = trim(I('post.activity_tel'));
                $activityModel->activity_introduce= trim(I('post.activity_introduce'));
                $activityModel->like = 0; // 
               $activityModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间
          $activityModel->spadmin_id = trim(I('post.spadmin_id'));
             if ($file['activity_picture']['size']==0) {//没有头像
                        $activityModel->activity_picture = "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有头像
                            $info=$this->MyUpload();
                            $activityModel->activity_picture= $info['activity_picture']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$activityModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('发布培训成功！', '../activitys/activitylist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($activityModel->getError());  
              }

    
    }



   
  public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/sportphoto/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                              // dump($info);    
                             //dump($info['picture']['savename']);
                             // 保存表单数据 包括附件数据
                         //处理头像图片

                $picinfo="Public/Uploads/sportphoto/".$info['activity_picture']['savename'];
                $image = new \Think\Image(); 
                $image->open($picinfo);
                // 生成一个居中裁剪为150*150的缩略图并保存
                 $image->save($picinfo);
                $image->open($picinfo)->text('AppStore','Public/assets_a/font/jinglei.ttf',20,'#000000',\Think\Image::IMAGE_WATER_SOUTHEAST)->save($picinfo); 
                return $info;
                         }
    }      





public function editactivity(){
    $activityid=$_GET['activity_id'];
    $activityModel=M('activitys');//表明,conf中定义前缀
    $activity= $activityModel->where("activity_id=$activityid")->select();

    //dump($user);
    $this->activity=$activity;//name属性对应的值 $this->对应值=$user;
    $this->display();
  }
   
   

public function updateactivity(){

      $data=$_POST;
      $file=$_FILES;
      
      $activityid=$data['activity_id'];

      $activityModel = M('activitys'); // 实例化sport模型
       
    if ($file['activity_picture']['size']==0) {
            //没有修改头像
            $result=$activityModel->where("activity_id=$activityid")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','PNG');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/sportphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
         //保存表单数据 包括附件数据
                $data['activity_picture']= $info['activity_picture']['savename']; 
                $result=$activityModel->where("activity_id=$activityid")->save($data);
                  if ($data['oldpic']!='nopic.jpg') {
                  $oldpicPath='Public/Uploads/sportphoto/'.$data['oldpic'];
                  unlink($oldpicPath);
                    }
                }
          }
    if($result){
           $this->success('修改成功！','../activitys/activitylist');
         }
          else{
            $this->error('修改失败！');
            }
  
  }

public function delactivity(){
  $activityid=$_GET['activity_id'];
 
  $activityModel = M("activitys"); // 实例化sport对象
  $activity=$activityModel->where("activity_id=$activityid")->select();
  $result=$activityModel->where("activity_id=$activityid")->delete(); 
 
    if ($activity[0]['activity_picture']!='nopic.jpg') {
              $picturePath='Public/Uploads/sportphoto/'.$activity[0]['activity_picture'];
              unlink($picturePath);
            }
    if($result){          
        $this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
      } else {
         $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
      }

}
 
}
 ?>